package com.service.luo;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dao.luo.CusDao;
import com.dao.luo.LinkmanDao;
import com.entity.Customer;
import com.entity.Linkman;
import com.service.ServiceException;

@Service
@Transactional(rollbackFor=Exception.class)
public class LinkmanService {
	@Autowired
	private LinkmanDao linkmanDao;
	@Autowired
	private CusDao cusDao;
	
	public Linkman findOne(Long linkno) {
		return linkmanDao.findOne(linkno);
	}
	
	public void inOrUpOne(Linkman linkman) {
		try {
			List<Customer> customers = linkman.getCustomers();
			List<Customer> cus = new ArrayList<Customer>();
			for(Customer cust : customers) {
				cus.add(cusDao.findOne(cust.getCusno()));
			}
			linkman.setCustomers(cus);
			linkmanDao.save(linkman);
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}
	
	public List<Customer> findAllCus(){
		List<Customer> list = new ArrayList<Customer>();
		Customer cus = null;
		while((cus=cusDao.findAll().iterator().next())!=null) {
			list.add(cus);
		};
		return list;
	}
	
	public Customer findOneCus(Long cusNo) {
		return cusDao.findOne(cusNo);
	}
}
